package com.example.administrator.qq;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.DB.DBManager;
import com.example.administrator.VO.UserInfo;
import com.example.administrator.VO.UserPoint;

import static com.example.administrator.Util.ShowToastTime.showMyToast;

/**
 * Created by tianqi on 2017/1/2.
 * Modified by ykbboom on 2017/1/3.
 */
public class StoreActivity extends Activity {
    private DBManager dbManager;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
        Button chargeBtn=(Button)findViewById(R.id.charge);//充值
        Button button1 = (Button) findViewById(R.id.button1);//初级体力球
        Button button2 = (Button) findViewById(R.id.button2);//中级体力球
        Button button3 = (Button) findViewById(R.id.button3);//高级体力球
        Button button4 = (Button) findViewById(R.id.button4);//金币球
        Button button5 = (Button) findViewById(R.id.button5);//黄金球
        Button button6 = (Button) findViewById(R.id.button6);//时间球
        Button button7 = (Button) findViewById(R.id.button7);//交换球
        Button button8 = (Button) findViewById(R.id.button8);//消除球
        Button button9 = (Button) findViewById(R.id.button9);//魔法球
        //跳转到充值界面
        chargeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(StoreActivity.this,VoucherActivity.class);
                startActivity(intent);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("初级体力球");
                dialog.setMessage("技能：加一点体力\n\n金币: 2个");
                dialog.setIcon(R.drawable.prop1);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dbManager = new DBManager(StoreActivity.this);
                        UserInfo userInfo = dbManager.queryAll();
                        if(userInfo.getMoney_num()>=2){
                            //更新金币数与体力数
                            userInfo.setBody_num(userInfo.getBody_num()+1);
                            userInfo.setMoney_num(userInfo.getMoney_num()-2);
                            dbManager.updateBodyNum(userInfo);
                            dbManager.updateMoneyNum(userInfo);
                            Toast toast=Toast.makeText(StoreActivity.this, "购买成功~", Toast.LENGTH_LONG);
                            showMyToast(toast,1*1000);
                        }else{
                            Toast toast=Toast.makeText(StoreActivity.this, "金币不足~", Toast.LENGTH_LONG);
                            showMyToast(toast,1*1000);
                        }
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {}
                });
                dialog.show();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("中级体力球");
                dialog.setMessage("技能：加三点体力\n\n金币: 5个");
                dialog.setIcon(R.drawable.prop2);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dbManager = new DBManager(StoreActivity.this);
                        UserInfo userInfo = dbManager.queryAll();
                        if(userInfo.getMoney_num()>=5){
                            //更新金币数与体力数
                            userInfo.setBody_num(userInfo.getBody_num()+3);
                            userInfo.setMoney_num(userInfo.getMoney_num()-5);
                            dbManager.updateBodyNum(userInfo);
                            dbManager.updateMoneyNum(userInfo);
                            Toast toast=Toast.makeText(StoreActivity.this, "购买成功~", Toast.LENGTH_LONG);
                            showMyToast(toast,1*1000);
                        }else{
                            Toast toast=Toast.makeText(StoreActivity.this, "金币不足~", Toast.LENGTH_LONG);
                            showMyToast(toast,1*1000);
                        }
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {}
                });
                dialog.show();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("高级体力球");
                dialog.setMessage("技能：加五点体力\n\n金币：8个");
                dialog.setIcon(R.drawable.prop3);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dbManager = new DBManager(StoreActivity.this);
                        UserInfo userInfo = dbManager.queryAll();
                        if(userInfo.getMoney_num()>=8){
                            //更新金币数与体力数
                            userInfo.setBody_num(userInfo.getBody_num()+5);
                            userInfo.setMoney_num(userInfo.getMoney_num()-8);
                            dbManager.updateBodyNum(userInfo);
                            dbManager.updateMoneyNum(userInfo);
                            Toast toast=Toast.makeText(StoreActivity.this, "购买成功~", Toast.LENGTH_LONG);
                            showMyToast(toast,1*1000);
                        }else{
                            Toast toast=Toast.makeText(StoreActivity.this, "金币不足~", Toast.LENGTH_LONG);
                            showMyToast(toast,1*1000);
                        }
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {}
                });
                dialog.show();
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("金币球");
                dialog.setMessage("技能：加100金币\n\n点券：3");
                dialog.setIcon(R.drawable.prop4);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast=Toast.makeText(StoreActivity.this, "点券不足请充值~", Toast.LENGTH_LONG);
                        showMyToast(toast,1*1000);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("黄金球");
                dialog.setMessage("技能：加300金币\n\n点券：10");
                dialog.setIcon(R.drawable.prop5);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast=Toast.makeText(StoreActivity.this, "点券不足请充值~", Toast.LENGTH_LONG);
                        showMyToast(toast,1*1000);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("时间球");
                dialog.setMessage("技能：加30秒时间\n\n点券：2");
                dialog.setIcon(R.drawable.prop6);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast=Toast.makeText(StoreActivity.this, "点券不足请充值~", Toast.LENGTH_LONG);
                        showMyToast(toast,1*1000);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("交换球");
                dialog.setMessage("技能：交换任意两个图标位置\n\n点券：5");
                dialog.setIcon(R.drawable.prop7);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast=Toast.makeText(StoreActivity.this, "点券不足请充值~", Toast.LENGTH_LONG);
                        showMyToast(toast,1*1000);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
        button8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("消除球");
                dialog.setMessage("技能：消除十字图标\n\n点券：10");
                dialog.setIcon(R.drawable.prop8);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast=Toast.makeText(StoreActivity.this, "点券不足请充值~", Toast.LENGTH_LONG);
                        showMyToast(toast,1*1000);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(StoreActivity.this);
                dialog.setTitle("魔法球");
                dialog.setMessage("技能：清除全部图标\n\n点券：20");
                dialog.setIcon(R.drawable.prop9);
                dialog.setCancelable(false);
                dialog.setPositiveButton("购买", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast toast=Toast.makeText(StoreActivity.this, "点券不足请充值~", Toast.LENGTH_LONG);
                        showMyToast(toast,1*1000);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });

    }
//    @Override
//    protected void onDestroy(){
//        super.onDestroy();
//        //应用的最后一个Activity关闭时应释放DB
//        dbManager.closeDB();
//    }
}
